/**
 * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.opsli.modulars.shop.shopconfig.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.opsli.api.wrapper.shop.shopconfig.ShopConfigModel;
import org.opsli.common.utils.WrapperUtil;
import org.opsli.core.base.service.impl.CrudServiceImpl;
import org.opsli.modulars.shop.shopconfig.entity.ShopConfig;
import org.opsli.modulars.shop.shopconfig.mapper.ShopConfigMapper;
import org.opsli.modulars.shop.shopconfig.service.IShopConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 店铺配置 Service Impl
 *
 * @author 17607080935
 * @date 2024-10-12 17:57:15
 */
@Service
public class ShopConfigServiceImpl extends CrudServiceImpl<ShopConfigMapper, ShopConfig, ShopConfigModel>
        implements IShopConfigService {

    @Autowired(required = false)
    private ShopConfigMapper mapper;

//    @Autowired
//    TenantApi tenantApi;

    /**
     * 按照当前位置查询最近店铺
     *
     * @param lat 经度
     * @param lon 维度
     * @return List<ShopConfigModel>
     */
    @Override
    public List<Map<String, Object>> listByDistance10(String lat, String lon, String id, String title) {
        QueryWrapper<ShopConfig> q = new QueryWrapper();
        if (!StrUtil.isEmpty(lat) && !StrUtil.isEmpty(lon)) {
            q.select("org_ids,id, FLOOR(ST_Distance_Sphere( POINT('" + lon + "','" + lat + "'), POINT(SUBSTRING_INDEX(value, ',', 1), SUBSTRING_INDEX(value, ',', -1)) )) AS is_default");
        }
        if (!StrUtil.isEmpty(id)) {
            q.like("org_ids", id);
        }
        if (!StrUtil.isEmpty(title)) {
            q.like("value", title);
            q.in("name", "title", "tag", "logLat");
        } else {
            q.eq("name", "logLat");
        }


//        q.or();
//        q.ne("tenant_id","0");
        q.orderByAsc("is_default");
        q.last("LIMIT 200");
        List<ShopConfigModel> list = WrapperUtil.transformInstance(mapper.selectList(q), ShopConfigModel.class);
        List<Map<String, Object>> list1 = new ArrayList<>();
        for (ShopConfigModel shopConfigModel : list) {
            QueryWrapper<ShopConfig> q2 = new QueryWrapper();
            q2.eq("org_ids", shopConfigModel.getOrgIds());
            List<ShopConfigModel> shopConfigModel2 = WrapperUtil.transformInstance(mapper.selectList(q2), ShopConfigModel.class);
            //查询租户是否启用。没启动就不展示
//            if(tenantApi.get(shopConfigModel.getTenantId()).getEnable().equals("0")){
//                continue;
//            }
            Map<String, Object> item = new HashMap<>();
            item.put("distance", shopConfigModel.getIsDefault());
            item.put("id", shopConfigModel.getOrgIds());
            for (ShopConfigModel configModel : shopConfigModel2) {
                //分割数组
                if (configModel.getType() == 5 && configModel.getValue() != null) {
                    item.put(configModel.getName(), configModel.getValue().split(","));
                } else {
                    item.put(configModel.getName(), configModel.getValue());
                }
            }
            list1.add(item);

        }
        return list1;

    }
}
